#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>

//主线程控制新线程
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;

void* active(void* args)
{
    std::string name = static_cast<const char*>(args);

    while(true)
    {
        pthread_mutex_lock(&gmutex);
        std::cout << name << " is active!" << std::endl;
        pthread_cond_wait(&gcond, &gmutex);
        pthread_mutex_unlock(&gmutex);
    }

    return (void*)0;
}

int main()
{
    pthread_t t1, t2, t3, t4;

    pthread_create(&t1, nullptr, active, (void*)"thread-1");
    pthread_create(&t2, nullptr, active, (void*)"thread-2");
    pthread_create(&t3, nullptr, active, (void*)"thread-3");
    pthread_create(&t4, nullptr, active, (void*)"thread-4");

    while(true)
    {
        sleep(1);
        pthread_cond_signal(&gcond);//主线程唤醒新线程
    }

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}